<template>
  <div class="dialog">
    <el-dialog :title="'直鱼率告警配置' + titleText" :visible.sync="showDialog"   :close-on-click-modal="false"
      @close="cancel">
      <el-form ref="ruleForm"  :model="formData" :rules="rules"  label-width="120px" class="demo-ruleForm">
        <el-form-item label="养殖品种:" prop="varietyId" >
          <el-select  class="w100p" v-model="formData.varietyId" placeholder="请选择" filterable clearable>
            <el-option v-for="(item, index) in varietyList" :label="item.varietyName" :value="item.fishVarietyId"
              :key="index + 'fishVariety'" />
          </el-select>
        </el-form-item>
        <el-form-item label="直鱼率:" prop="deadRatio">
          <el-input v-model="formData.deadRatio" type="number" clearable placeholder="标准最大直鱼率%">
            <template slot="append">%</template>
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button class="cancelbtn" @click="cancel()">取 消</el-button>
        <el-button type="primary" class="savebtn" @click="save('ruleForm')">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import DeadAlarmConfigApi from "@/api/health-management/dead-alarm-config";
import FishVarietyApi from "@/api/basic-info-manage/fishVariety";
import EnumApi from "@/api/enum";
import { dialogMixin } from "@/utils/dialogMixin"
export default {
  name: '',
  components: {},
  mixins: [dialogMixin],
  props: {
    // 弹框名称
    titleText: {
      type: String,
      default: ""
    },
    nowChanceStau: {
      type: String,
      default: ""
    },
    show: {
      type: Boolean,
      default: false
    },
    editObject: {
      type: [Object],
      default: () => {
        return {}
      }
    },
  },
  data() {
    let checkInt = (rule, value, callback) => {
      console.log(value);
      if (value) {
        if (Number(value) && value > 0) {
          callback();
        } else {
          callback(new Error('请输入大于0的数'));
        }
      } else {
        callback(new Error(rule.emptyMsg));
      }
    };
    return {
      formData: {},
      rules: {
        deadRatio: [
          { required: true, emptyMsg: "请输入直鱼率", trigger: ["change", "blur"], validator: checkInt }
        ],
        varietyId: [
          { required: true, message: "请选择品种", trigger: ["change", "blur"] }
        ],
      },
      materialUnit: undefined,
      materialList: [],
      staffList: [],
      varietyList: [],
      showDialog: this.show
    };
  },
  watch: {
    show(v) {
      this.showDialog = v;
    },
    nowChanceStau(v) {
      if (v === 'add') {
        this.formData = {
        }
        this.$nextTick(() => {
          this.$refs['ruleForm'].clearValidate()
        })
      }
      if (v === 'edit') {
        this.formData = this.editObject
      }
    }
  },
  created() {
  },
  mounted() {
    this.queryFishVariety()
  },
  methods: {
    queryFishVariety() {
      FishVarietyApi.queryByCondition().then(res => {
        this.varietyList = res.data;
      })
    },
    save(formData) {
      this.$refs[formData].validate(valid => {
        if (valid) {
          if (this.nowChanceStau === "edit") {
            DeadAlarmConfigApi.edit(this.formData).then(res => {
              if (res.success) {
                this.$message.success("修改成功！");
                this.close()
              } else {
                this.$message.error("系统错误,请稍后再试");
              }
            });
          } else if (this.nowChanceStau === "add") {
            DeadAlarmConfigApi.add(this.formData).then(res => {
              if (res.success) {
                this.$message.success("添加成功");
                this.close()
              } else if (res.success === false && res.errCode === 200) {
                this.$message.error(res.message);
              } else this.$message.error("系统错误,请稍后再试");
            });
          }
        } else {
          return false;
        }
      });
    },
    close() {
      this.$emit("closeDialog");
    },
    cancel() {
      this.$emit("cancelDialog");
    }
  }
};
</script>

<style lang="scss" scoped></style>
